<?php
namespace app\system\controller;

/**
 * 提现管理
 *
 * @ author lee
 * */
 
class Msg extends Common
{

    protected $pageSize = 20;

    /**
     * 通知列表
     * @param string $keywords
     * @return mixed
     */
    public function index($keywords='')
    {
        $where = ['user_id'=>0];
        !empty($keywords) && $where['u.title'] = ['like', "%$keywords%"];

        $pageconfig['path'] = url("?keywords=$keywords");
        $withdrawModel = new \app\common\model\Msg();
        $list = $withdrawModel->getList($where, $this->pageSize, $pageconfig, false, 'id desc');
        $assign = [
            'list' => $list
        ];
        return $this->fetch('', $assign);
    }

    /**
     * 删除
     * @param int $id
     * @return \think\response\Json
     */
    public function del($id)
    {
        if (isAjaxPost()) {
            $id = intval($id);
            if ($id <= 0) {
                return $this->jsonError('删除失败');
            }

            $model = new \app\common\model\Msg();
            if ($model->where(['id'=>$id])->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }

    /**
     * 批量删除
     * */
    public function delAll()
    {
        if (isAjaxPost()) {
            $post = input('post.');

            if (empty($post['id']) || !is_array($post['id']) || count($post['id']) <= 0) {
                return $this->jsonError('请勾选需要删除的通知！');
            }
            $ids = $post['id'];

            $model = new \app\common\model\Msg();
            if ($model->where(['id'=>['in', $ids]])->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }

    /**
     * 添加编辑
     * @param int $id
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save($id=0) {
        $model = new \app\common\model\Msg();
        if (isAjaxPost()) {
            $title = input('post.title', '', 'trim');
            $content = input('post.content', '', 'trim');

            if (empty($title)) {
                return $this->jsonError('请输入标题');
            }
            if (empty($content)) {
                return $this->jsonError('请输入内容');
            }

            if ($id > 0) {
                if ($model->update(['title'=>$title,'content'=>$content], ['id'=>$id]) !== false) {
                    return $this->jsonSuccess('修改成功');
                } else {
                    return $this->jsonError('修改失败');
                }
            } else {
                if ($model->insertMsg($content, 0, $title)) {
                    return $this->jsonSuccess('添加成功');
                } else {
                    return $this->jsonError('添加失败');
                }
            }
        }

        if ($id > 0) {
            $this->assign('data', $model->where(['id'=>$id])->find());
        }

        return $this->fetch();
    }

}
